package smartcounters.androidclient;

import smartcounters.androidclient.WebServices.InformationUpdaterWebServiceClient;
import smartcounters.androidclient.WebServices.OnResultReceiver;
import smartcounters.androidclient.model.Configuration;
import smartcounters.androidclient.model.eOperationsEnum;
import android.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AccountDetailsActivity extends Activity implements OnClickListener, OnResultReceiver{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_details_layout);
        
        Button updateAccountButton = (Button)findViewById(R.id.buttonUpdateAccount);
        updateAccountButton.setOnClickListener(this);
        
        Button deleteAccountButton = (Button)findViewById(R.id.buttonDeleteAccount);
        deleteAccountButton.setOnClickListener(this);
        
        EditText accountIDEditText = (EditText)findViewById(R.id.accountDetailsIDEditText);
        EditText countryEditText = (EditText)findViewById(R.id.accountDetailsCountryEditText);
		EditText cityEditText = (EditText)findViewById(R.id.accountDetailsCityEditText);
		EditText streetEditText = (EditText)findViewById(R.id.accountDetailsStreetEditText);
		EditText houseEditText = (EditText)findViewById(R.id.accountDetailsHouseEditText);
		EditText apartmentEditText = (EditText)findViewById(R.id.accountDetailsApartmentEditText);
		EditText accountPhoneEditText = (EditText)findViewById(R.id.accountDetailsPhoneEditText);
		EditText usersAssociatedWithEditText = (EditText)findViewById(R.id.accountDetailsUsersAssociatedWithEditText);
		
		accountIDEditText.setText(String.valueOf(FindAccountResultsActivity.s_Account.getAccountID()));
		countryEditText.setText(FindAccountResultsActivity.s_Account.getCountry());
		cityEditText.setText(FindAccountResultsActivity.s_Account.getCity());
		streetEditText.setText(FindAccountResultsActivity.s_Account.getStreet());
		houseEditText.setText(String.valueOf(FindAccountResultsActivity.s_Account.getHouse()));
		if (FindAccountResultsActivity.s_Account.getApartment() != -1){
			apartmentEditText.setText(String.valueOf(FindAccountResultsActivity.s_Account.getApartment()));
		}
		accountPhoneEditText.setText(FindAccountResultsActivity.s_Account.getPhone());
		
		String usersAssociatedWith = "";
		for (int user : FindAccountResultsActivity.s_Account.getUsersAssociatedWith()){
			usersAssociatedWith += String.valueOf(user) + ",";
		}
		
		if (usersAssociatedWith.length() > 0){
			usersAssociatedWith = usersAssociatedWith.substring(0, usersAssociatedWith.length() - 1);
		}
		
		usersAssociatedWithEditText.setText(usersAssociatedWith);
    }

	public void onClick(View v) {
		if (v.getId() == R.id.buttonUpdateAccount){
			EditText accountIDEditText = (EditText)findViewById(R.id.accountDetailsIDEditText);
			EditText countryEditText = (EditText)findViewById(R.id.accountDetailsCountryEditText);
			EditText cityEditText = (EditText)findViewById(R.id.accountDetailsCityEditText);
			EditText streetEditText = (EditText)findViewById(R.id.accountDetailsStreetEditText);
			EditText houseEditText = (EditText)findViewById(R.id.accountDetailsHouseEditText);
			EditText apartmentEditText = (EditText)findViewById(R.id.accountDetailsApartmentEditText);
			EditText accountPhoneEditText = (EditText)findViewById(R.id.accountDetailsPhoneEditText);
			EditText usersAssociatedWithEditText = (EditText)findViewById(R.id.accountDetailsUsersAssociatedWithEditText);
			Button updateAccountButton = (Button)findViewById(R.id.buttonUpdateAccount);
	        Button deleteAccountButton = (Button)findViewById(R.id.buttonDeleteAccount);
			
	        int[] users = null;
			
			String[] usersStrings = usersAssociatedWithEditText.getText().toString().split(",");
			if (usersStrings.length == 1 && usersStrings[0].length() == 0)
			{
				users = new int[0];
			}
			else
			{
				users = new int[usersStrings.length];
				for (int i = 0; i < usersStrings.length; i++){
					users[i] = Integer.parseInt(usersStrings[i]);
				}	
			}
			
			InformationUpdaterWebServiceClient updaterClient = new InformationUpdaterWebServiceClient(Configuration.getInstance().getServerAddress());
			
			countryEditText.setEnabled(false);
			cityEditText.setEnabled(false);
			streetEditText.setEnabled(false);
			houseEditText.setEnabled(false);
			apartmentEditText.setEnabled(false);
			accountPhoneEditText.setEnabled(false);
			usersAssociatedWithEditText.setEnabled(false);
			updateAccountButton.setEnabled(false);
			deleteAccountButton.setEnabled(false);
			
			updaterClient.RegisterResultReceiver(this);
			
			TextView messageTextView = (TextView)findViewById(R.id.accountDetailsMessageTextView);
			messageTextView.setText("Please wait...");
			
			int apartment = -1;
			
			try{
				apartment = Integer.parseInt(apartmentEditText.getText().toString());
			}
			catch (Exception ex){
				apartment = -1;
			}
			
			if (apartment == -1){
				updaterClient.UpdateAccountWithoutApartment(
						Configuration.getInstance().getUserCredentials(),
						Integer.parseInt(accountIDEditText.getText().toString()),
						countryEditText.getText().toString(),
						cityEditText.getText().toString(),
						streetEditText.getText().toString(),
						Integer.parseInt(houseEditText.getText().toString()),
						accountPhoneEditText.getText().toString(),
						users);
			}
			else{
				updaterClient.UpdateAccountWithApartment(
						Configuration.getInstance().getUserCredentials(),
						Integer.parseInt(accountIDEditText.getText().toString()),
						countryEditText.getText().toString(),
						cityEditText.getText().toString(),
						streetEditText.getText().toString(),
						Integer.parseInt(houseEditText.getText().toString()),
						Integer.parseInt(apartmentEditText.getText().toString()),
						accountPhoneEditText.getText().toString(),
						users);
			}
		}
		else if (v.getId() == R.id.buttonDeleteAccount){
			EditText accountIDEditText = (EditText)findViewById(R.id.accountDetailsIDEditText);
			
			TextView messageTextView = (TextView)findViewById(R.id.accountDetailsMessageTextView);
			messageTextView.setText("Please wait...");
			
			InformationUpdaterWebServiceClient updaterClient = new InformationUpdaterWebServiceClient(Configuration.getInstance().getServerAddress());
			updaterClient.RegisterResultReceiver(this);
			
			updaterClient.DeleteAccount(Configuration.getInstance().getUserCredentials(), Integer.parseInt(accountIDEditText.getText().toString()));
		}
	}

	public void ReceiveResult(Message i_Msg) {
		EditText countryEditText = (EditText)findViewById(R.id.accountDetailsCountryEditText);
		EditText cityEditText = (EditText)findViewById(R.id.accountDetailsCityEditText);
		EditText streetEditText = (EditText)findViewById(R.id.accountDetailsStreetEditText);
		EditText houseEditText = (EditText)findViewById(R.id.accountDetailsHouseEditText);
		EditText apartmentEditText = (EditText)findViewById(R.id.accountDetailsApartmentEditText);
		EditText accountPhoneEditText = (EditText)findViewById(R.id.accountDetailsPhoneEditText);
		EditText usersAssociatedWithEditText = (EditText)findViewById(R.id.accountDetailsUsersAssociatedWithEditText);
		Button updateAccountButton = (Button)findViewById(R.id.buttonUpdateAccount);
        Button deleteAccountButton = (Button)findViewById(R.id.buttonDeleteAccount);
		
        if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.UpdateAccountWithoutApartment){
        	TextView messageTextView = (TextView)findViewById(R.id.accountDetailsMessageTextView);
			messageTextView.setText("The account was updated.");
        }
        else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.UpdateAccountWithApartment){
        	TextView messageTextView = (TextView)findViewById(R.id.accountDetailsMessageTextView);
			messageTextView.setText("The account was updated.");
        }
        else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.DeleteAccount){
        	TextView messageTextView = (TextView)findViewById(R.id.accountDetailsMessageTextView);
			messageTextView.setText("The account was deleted.");
			
			Intent intent = new Intent();
			intent.setClass(this, MainActivity.class);
			startActivity(intent);
        }
        
        countryEditText.setEnabled(true);
		cityEditText.setEnabled(true);
		streetEditText.setEnabled(true);
		houseEditText.setEnabled(true);
		apartmentEditText.setEnabled(true);
		accountPhoneEditText.setEnabled(true);
		usersAssociatedWithEditText.setEnabled(true);
		updateAccountButton.setEnabled(true);
		deleteAccountButton.setEnabled(true);
	}
}